<?php

function extractDateTime($data, $property, $field) {
	global $tz_array, $phpiCal_config, $calendar_tz;
	$allday =''; #suppress error on returning undef.
	// Check for zulu time.
	$zulu_time = false;
//	echo "data=".$data."<br>";
	if (substr($data,-1) == 'Z') $zulu_time = true;
	// Pull out the timezone, or use GMT if zulu time was indicated.
	if (preg_match('/^'.$property.';TZID=/i', $field)) {
		$tz_tmp = explode('=', $field);
		$tz_dt = match_tz($tz_tmp[1]); //echo "$tz_dt<br>";
	} elseif ($zulu_time) {
		$tz_dt = 'GMT';echo $tz_dt."</br>";
	}
	// Extract date-only values.
	if ((preg_match('/^'.$property.';VALUE=DATE:/i', $field))){ // || ( preg_match('^([0-9]{4})([0-9]{2})([0-9]{2})$', $data)))  
		// Pull out the date value. Minimum year is 1970.
		preg_match ('([0-9]{4})([0-9]{2})([0-9]{2})', $data, $dt_check);
		if ($dt_check[1] < 1970) { 
			$dt_check[1] = '1970';
		}		
		# convert to date-time
		//$data = $dt_check[1].$dt_check[2].$dt_check[3]."T000000";
		$data = str_replace('Z', '', $data);
		$time = '';
		$allday = $data;
	}
	$data = str_replace('Z', '', $data);
	//echo();
	// Extract date-time values.
	// Pull out the date and time values. Minimum year is 1970.
	preg_match ('/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{0,2})([0-9]{0,2})([0-9]{0,2})/', $data, $regs);
	if (!isset ($regs[1])) return;
	if ($regs[1] < 1970) { 
		$regs[1] = '1970';
	}
	$date = array();
	$date["jour"] = $regs[3];
	$date["mois"] = $regs[2];
	$date["annee"] = $regs[1];
	
	
	$date["heure"] = $regs[4];
	$date["minute"] = $regs[5];
	$date["seconde"] = $regs[6];
	
	//echo $regs[1]."<br>";
	
	//return $regs[3]."/".$regs[2]."/".$regs[1];
	return $date;
	
	
	//echo $data." ".$property." ".$field."<br>";
}

/* TZIDs in calendars often contain leading information that should be stripped
Example: TZID=/mozilla.org/20050126_1/Europe/Berlin
if this has been set by the parse_tzs scanning the file, then it should be OK, but sometimes a calendar may have a tzid without having defined the vtimezone, expecting a match (This will often happen when users send isolated events in bug reports; the calendars should have vtimezones).

Need to return the part that matches a key in $tz_array
*/
//DTSTART;TZID=Europe/Paris:20101007T083000
function match_tz($data){
	global $tz_array;
	if (isset($tz_array[$data])) return $data;
	foreach ($tz_array as $key=>$val){
		if (strpos(" $data",$key) > 0) return $key;
	}
	return $data;
}



?>